define(function(require) {

    // 其他模块引用的对象将作为属性统一放到_module中
    var _module = {
        conflictValidator: require('./conflictValidator')
    };

    var commonLib = {

        /**
         * 获取uuid
         * @returns {string}
         */
        getUuid: function() {
            var s = [];
            var hexDigits = "0123456789abcdef";
            for (var i = 0; i < 36; i++) {
                s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
            }
            s[14] = "4";
            s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
            s[8] = s[13] = s[18] = s[23] = "";
            return s.join("");
        },

        /**
         * 比对两个“01”串，是否存在交集，如果同时为1表示有交集
         * @param  {[type]}  nExpression1 [description]
         * @param  {[type]}  nExpression2 [description]
         * @return {Boolean}              [description]
         */
        hasIntersection: function(nExpression1, nExpression2) {
            var nExpression1Arr = nExpression1.split('');
            var nExpression2Arr = nExpression2.split('');
            for (var i = 0; i < nExpression1Arr.length; i++) {
                if (nExpression1Arr[i] === '1') {
                    if (nExpression2[i] !== undefined && nExpression2[i] === '1') {
                        return true;
                    }
                }
            }
            return false;
        },

        /**
         * 根据星期、开始结束节次获取对应的排课结果列表
         * @param  {[type]} arranges  [description]
         * @param  {[type]} week      [description]
         * @param  {[type]} beginUnit [description]
         * @param  {[type]} endUnit   [description]
         * @return {[type]}           [description]
         */
        getArranges: function(arranges, week, beginUnit, endUnit) {
            var _arranges = [];
            for (var i = 0; i < arranges.length; i++) {
                if (arranges[i].week === week && arranges[i].beginUnit <= endUnit && arranges[i].endUnit >= beginUnit) {
                    _arranges.push(arranges[i]);
                }
            }
            return _arranges;
        },

        /**
         * 根据周次、星期、开始结束节次获取对应的屏蔽区域列表
         * @param  {[type]} obstacles [description]
         * @param  {[type]} weekNum   [description]
         * @param  {[type]} week      [description]
         * @param  {[type]} beginUnit [description]
         * @param  {[type]} endUnit   [description]
         * @return {[type]}           [description]
         */
        getObstacles: function(obstacles, weekNum, week, beginUnit, endUnit) {
            var _obstacles = [];
            if (typeof obstacles === 'undefined' || typeof weekNum === 'undefined') {
                return _obstacles;
            }

            for (var i = 0; i < obstacles.length; i++) {
                var valid = _module.conflictValidator.validateTimeObj(obstacles[i], { weekNum: weekNum, week: week, beginUnit: beginUnit, endUnit: endUnit });
                if (valid) {
                    _obstacles.push(obstacles[i]);
                }
            }
            return _obstacles;
        },

        /**
         * 根据星期、开始节次、结束节次找到对应的td jdom原色
         * @param  {[type]} week      [description]
         * @param  {[type]} beginUnit [description]
         * @param  {[type]} endUnit   [description]
         * @return {[type]}           [description]
         */
        findJItem: function(week, beginUnit, endUnit) {
            var item = undefined;
            $('[data-role="item"]').each(function() {
                var jobj = $(this);
                if (jobj.attr('data-week') === week && jobj.attr('data-begin-unit') === beginUnit && jobj.attr('data-end-unit') === endUnit) {
                    item = jobj;
                }
            });
            return item;
        },

        /**
         * 检查当前的排课结果是否与原始结果一致，用于判断排课结果是否修改
         * @param  {[type]} arrange  [description]
         * @param  {[type]} arranges [description]
         * @return {[type]}          [description]
         */
        checkArrangeInArranges: function(arrange, arranges) {
            var isIn = false;
            for (var i = 0; i < arranges.length; i++) {
                var a = arranges[i];
                if (this.validObjIsSame(arrange, a, ['jxbid', 'teacherId', 'teacherName', 'classroomId', 'classroomName', 'weekNum', 'week', 'beginUnit', 'endUnit'])) {
                    isIn = true;
                    break;
                }
            }
            return isIn;
        },

        /**
         * 根据属性验证两个对象是否相同
         * @param  {[type]} obj1       [description]
         * @param  {[type]} obj2       [description]
         * @param  {[type]} properties [description]
         * @return {[type]}            [description]
         */
        validObjIsSame: function(obj1, obj2, properties) {
            var same = true;
            for (var i = 0; i < properties.length; i++) {
                if (obj1[properties[i]] !== obj2[properties[i]]) {
                    same = false;
                    break;
                }
            }
            return same;
        },

        /**
         * 数组及对象的深拷贝
         * @param  {[type]} o [description]
         * @return {[type]}   [description]
         */
        deepCopy: function(o) {
            if (o instanceof Array) {
                var n = [];
                for (var i = 0; i < o.length; ++i) {
                    n[i] = this.deepCopy(o[i]);
                }
                return n;
            } else if (o instanceof Object) {
                var n = {}
                for (var i in o) {
                    n[i] = this.deepCopy(o[i]);
                }
                return n;
            } else {
                return o;
            }
        },

        /**
         * 对象内容复制
         * @param from
         * @param to
         */
        copyPropertyVal: function(from, to) {
            for (var f in from) {
                for (var t in to) {
                    if (f == t && from[f] != null) {
                        to[t] = from[f];
                        break;
                    }
                }
            }
        }
    };

    return commonLib;

});
